﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ClassLibrary.DAO;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Collections.Generic;


public partial class Client_UserControl_ucItemDetail : System.Web.UI.UserControl
{
    TblItemsDAO ItemsDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblItemDetailDAO itemDetailDAO = new TblItemDetailDAO(Global.CONNECTION_STRING);
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            if (id == null || id == "")
                Response.Redirect("~/Products.aspx");
            LoadDetails();

            LoadData("","","");
      
        }
    }

    private void LoadDetails()
    {
        string id = Request.QueryString["id"];
        //frmViewDetail.DataSource = ItemsDAO.GetListItems(Convert.ToInt32(id), "", "", 1, 10, "");
        frmViewDetail.DataSource = ItemsDAO.GetvItemByID(Convert.ToInt32(id));
        frmViewDetail.DataBind();
    }
    protected void btnvote_Click(object sender, ImageClickEventArgs e)
    {
         string id = Request.QueryString["id"];

        Label lbVote = (Label)frmViewDetail.FindControl("lbvote");
        int lb = Global.ConvertToInt(lbVote.Text);
        lb += 1;
        lbVote.Text = lb.ToString();

        TblItemDetail item_detail = new TblItemDetail();

        item_detail.Vote = lb;
        itemDetailDAO.UpdateVote(Global.ConvertToInt(id), lb);


    }
    protected void cmdMua_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Label lblInfoAddCart1 = (Label)frmViewDetail.FindControl("lblInfoAddCart");
            lblInfoAddCart1.Text = "";

            string id = Request.QueryString["id"];
            BasePage.SetShoppingCartSesstion(id + "|0");

            List<TblOrderDetail> arrItems = new List<TblOrderDetail>();
            if (Session[Global.SESSION_CART] != null)
                arrItems = (List<TblOrderDetail>)Session[Global.SESSION_CART];

            int iNoOfItems = 0;
            double dTotalAmount = 0;

            foreach (TblOrderDetail order in arrItems)
            {
                iNoOfItems += order.Quantity;
                dTotalAmount += Convert.ToDouble(order.Quantity) * Convert.ToDouble(order.Price);
            }
            lblInfoAddCart1.Text = "Đã thêm vào giỏ hàng ! ";

            ScriptManager.RegisterStartupScript(frmViewDetail ,
            this.frmViewDetail.GetType(), "ScriptUpdateParents", "UpdateParentsCart('" + iNoOfItems.ToString() + "|" + dTotalAmount.ToString("0,00.##") + "');", true);

            //Response.Redirect("Default.aspx");
           // ScriptManager.RegisterClientScriptBlock(frmViewDetail, this.frmViewDetail.GetType(), "ScriptUpdateParents", "UpdateParentsCart('" + iNoOfItems.ToString() + "|" + dTotalAmount.ToString("0,00.##") + "');", true);
          
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }
    protected void frmViewDetail_DataBound(object sender, EventArgs e)
    {
  
    }

   
    protected void recomment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

    public void LoadData(string strName, string strCatID, string strOrderby)
    {
        string idgroup = Request.QueryString["idgroup"].ToString();
        try
        {
            lvItems.DataSource = ItemsDAO.GetProduct(0, strName, strCatID, 1, 0, 9, "", Global.ConvertToInt(idgroup));
            lvItems.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }
    protected void lvItems_PagePropertiesChanged(object sender, EventArgs e)
    {
        lvItems.DataBind();
    }
}
